﻿using LearnMAUI2.Pages;
using LearnMAUI2.Services;

namespace LearnMAUI2
{
    public partial class AppShell : Shell
    {
        //AppShell相当于App的内部Shell，App本身可以认为是一个窗口，就是MAUI app整体
        //Page是要显示的内容，隶属于App
        public AppShell()
        {
            InitializeComponent();
            //获取XAML中的ServiceLocator资源
            var serviceLocatorName=nameof(ServiceLocator);
            //从页面获取资源的写法，对应APP.XAML中的
            /*
             *  <ResourceDictionary>
                    <local:ServiceLocator x:Key="ServiceLocator"></local:ServiceLocator>
                </ResourceDictionary>
             */
            var serviceLocator = (ServiceLocator)Application.Current.Resources.MergedDictionaries.
                First(x => x.ContainsKey(serviceLocatorName))[serviceLocatorName];

            //注册后可以从SeriviceLocator获得路由服务
            var routeService=serviceLocator.RouteService;


            //临时用法，不在真正的导航中使用
            //有点类似于菜单栏，默认的菜单栏，FlyoutItem用于在导航菜单栏中的页面注册,如果有2个ResultPage，要么干掉一个，要么改成AddScoped
            //Items.Add(new FlyoutItem
            //{
            //    Title = nameof(ResultPage),
            //    Route = nameof(ResultPage),
            //    Items = {
            //        //当点击具体的导航页时会触发创建实例，实际上是一个反射的动作
            //        new ShellContent
            //        {
            //            ContentTemplate=new DataTemplate(typeof(ResultPage)),
            //        }
            //    }
            //});
            //新增Today Page
            Items.Add(new FlyoutItem
            {
                Title = nameof(TodayPage),
                Route = routeService.GeteRoute(RootNavigationConstant.TodayPage),//nameof(TodayPage),
                Items = {
                    new ShellContent
                    {
                        ContentTemplate=new DataTemplate(typeof(TodayPage)),
                    }
                }
            });
            Items.Add(new FlyoutItem
            {
                Title = nameof(QueryPage),
                Route = routeService.GeteRoute(RootNavigationConstant.QueryPage),//nameof(TodayPage),
                Items = {
                    new ShellContent
                    {
                        ContentTemplate=new DataTemplate(typeof(QueryPage)),
                    }
                }
            });
            //TODO
            Items.Add(new FlyoutItem
            {
                Title = nameof(ResultPage),
                Route = routeService.GeteRoute(ContentNavigationConstant.ResultPage),//nameof(TodayPage),
                Items = {
                    new ShellContent
                    {
                        ContentTemplate=new DataTemplate(typeof(ResultPage)),
                    }
                }
            });

            //非导航菜单栏中的页面注册使用Routing.RegisterRoute,FlyoutItem最后也会注册到Routing中
            Routing.RegisterRoute(routeService.GeteRoute(RootNavigationConstant.TodayPage),typeof(TodayDetailPage));
            //TODO
            Routing.RegisterRoute(routeService.GeteRoute(ContentNavigationConstant.DetailPage), typeof(DetailPage));
        }
    }
}
